package ext.tianma.part.forms;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import wt.fc.ReferenceFactory;
import wt.fc.WTObject;
import wt.fc.collections.WTArrayList;
import wt.folder.Folder;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.pom.Transaction;
import wt.session.SessionHelper;
import wt.util.LocalizableMessage;
import wt.util.WTException;
import wt.util.WTMessage;
import wt.util.WTProperties;
import wt.util.WTPropertyVetoException;

import com.ptc.core.components.forms.DefaultEditFormProcessor;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.forms.FormResultAction;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmStandardDateFormat.form;

public class BatchUpdatePartsProcessor extends  DefaultObjectFormProcessor
{
	public BatchUpdatePartsProcessor(){}
	
	private static Locale locale = null;
	private static String CLASSNAME = BatchUpdatePartsProcessor.class.getName();
	private static boolean VERBOSE = false;
	static {
		try {
			VERBOSE = (WTProperties.getLocalProperties()).getProperty("ext.tianma.excel.verbose", false);
			locale = SessionHelper.manager.getLocale();
		} catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	
	// 输出日志
	public static void outDebugInfo(String info) {
		if (VERBOSE)
			System.out.println("<<< " + CLASSNAME + "." + info);
	}
	public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException
	{
		FormResult formresult = new FormResult();
		formresult.setNextAction(FormResultAction.REFRESH_OPENER);
		formresult.setStatus(FormProcessingStatus.SUCCESS);
		Transaction trx= new Transaction();
		try {
		String fileID="file";
		File file = (File)((HashMap)(nmcommandbean.getMap().get("fileUploadMap"))).get(fileID);
		
		HttpServletRequest httpservletrequest = nmcommandbean.getRequest();
		File file1 = (File) httpservletrequest.getAttribute("file");
		outDebugInfo("file="+file1);
		
		if(file1!=null)
		{
			String path=file1.getAbsolutePath();
			System.out.println(" path="+path);
			
				WTArrayList partlist = null;
				
				try {
					trx.start();
					outDebugInfo("------------------------------- trx :"+trx);
					partlist = ReadExcel.readSheet(path);
					trx.commit();
			         trx=null;
				} catch (WTPropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace(); 
				}
				outDebugInfo(" partlist.size()="+partlist.size());
			
		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			 if(trx!=null)
			 {
                System.out.println("出现错误,进行回滚.");
                trx.rollback();
             }
		}
		
		FeedbackMessage feedbackmessage = new FeedbackMessage(FeedbackType.SUCCESS, locale, null, null,
				new LocalizableMessage[] { new WTMessage("ext.tianma.part.partResource", "updatesuccess", null) });
        formresult.addFeedbackMessage(feedbackmessage);
		super.doOperation(nmcommandbean, list);
		return formresult;
	}
	
	
	/**
     *根据oid获取Windchill对象
     * @author Harry Cao
     * @param oid
     * @return
     * @throws WTException
     */
    public static WTObject getWTObjectByOid(String oid) throws WTException
    {
        ReferenceFactory rf = new ReferenceFactory();
        return (WTObject) rf.getReference(oid).getObject();
    }

}
